//
//  ProductViewController.m
//  BP_NewClient
//
//  Created by normal on 15-4-17.
//  Copyright (c) 2015年 normal. All rights reserved.
//  门店详情

#import "ProductViewController.h"
#import "BPCommon.h"
#import "BURequestManager.h"
#import "UIImageView+AFNetworking.h"
#import "AppDelegate.h"
#import "BP_NewClient-Swift.h"
#import "ProductInfoCell.h"
#import "NSString+ChatRoom.h"
@interface ProductViewController ()

@end

@implementation ProductViewController

//    var mapManager = MALocationManager.shareManager()
//    mapManager.MASearchPath(self)


- (IBAction)mapAddressAction:(id)sender{
   MALocationManager *mapmManager = [MALocationManager shareManager];
    [mapmManager MASearchPath:self];
}


- (void)back:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)share:(id)sender{

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[AppDelegate shareApplication] hidesTabBar:YES animated:YES];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"佳格专业美发沙龙";
    self.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK( @selector(back:));
    self.navigationItem.rightBarButtonItem = BAR_BUTTON_IMAGE(@"product_share", @selector(share:));
    self.view.backgroundColor = [UIColor whiteColor];
    
    locationAddress.textColor = [BPCommon colorWithHexValue:0x666666 alpha:1.0f];
    distanceLabel.text = self.model.distance;
    
    
    headerView.frame = CGRectMake(0, 0, ScreenWidth, 200);
    [self setTableViewHeightWithTabBarhidden:YES];
    self._tableView.tableHeaderView = headerView;
    [self requestStoreProduct];
    [self addGetMoreFootView];
}

- (void)addGetMoreFootView{

}

// 请求数据
- (void)requestStoreProduct{
    
    NSDictionary *dic = @{@"store_id":[NSString safeString:self.model.id]};
  
    [[BURequestManager sharedRequest] requestGetWithParameterWithBlockDic:STORE_PRODUCT_LIST SetParamer:dic success:^(NSDictionary *dic){
        
        self.productModel = [[ProductListModel alloc] initWithDictionary:[dic objectForKey:@"data"] error:nil];
      
        [bigImage setImageWithURL1:[NSURL URLWithString:self.productModel.storeImg]
        placeholderImage:[UIImage imageNamed:@"2.jpg"]];
        
        locationAddress.text = self.productModel.address;
        storeName.text = self.productModel.name;
        
        [self._tableView reloadData];
        
    } failure:^(NSString *errMsg){
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger count = self.productModel.product_list.count;
    return count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.productModel.product_list.count) {
        return 55;
    }else{
        return 95;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identy = @"ProductInfoCell";
    ProductInfoCell  *cell = [tableView dequeueReusableCellWithIdentifier:identy];
    
    NSArray *nibs;
    if (nil == cell) {
        nibs = [[NSBundle mainBundle]loadNibNamed:@"ProductInfoCell" owner:self options:nil];
    }
    
    if (indexPath.row == self.productModel.product_list.count) {
        cell = [nibs objectAtIndex:1];
        
    } else{
        cell = [nibs objectAtIndex:0];
        
        if (indexPath.row < self.productModel.product_list.count) {
            ProductModel *product = [self.productModel.product_list objectAtIndex:indexPath.row];
            [cell.image setImageWithURL1:[NSURL URLWithString:product.product_img] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
            cell.title.text = [NSString stringWithFormat:@"%@",product.name];
            cell.price.text =  [NSString stringWithFormat:@"¥:%@",product.price];
            cell.distancePrice.text = [NSString stringWithFormat:@"已售:%@",product.sale_count];
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ProductModel *product = [self.productModel.product_list objectAtIndex:indexPath.row];

    ProductDetailViewController *productDetail = [[ProductDetailViewController alloc] initWithNibName:@"ProductDetailViewController" bundle:nil];
    productDetail.productModel = product;
    productDetail.productDetailBlock = ^(NSString *str){
        NSLog(@"%@",str);
    };
    
    [self.navigationController pushViewController:productDetail animated:YES];
}


// 拨打电话
-(IBAction)telBtnClicked:(id)sender{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"024-2136996"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"拨打电话",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
